import javax.swing.*;
import java.awt.*;

public class LoginView extends JFrame {
    JLabel nameLabel = new JLabel("chatDistance Client", SwingConstants.CENTER);
    SpringLayout springLayout = new SpringLayout();
    JPanel centerPanel = new JPanel(springLayout);
    JLabel userNameLabel = new JLabel("用户名：");
    JTextField userNameField = new JTextField();
    JLabel passwordLabel = new JLabel("密码：");
    JPasswordField passwordField = new JPasswordField();
    JButton loginButton = new JButton("登录");
    JButton registerButton = new JButton("点此进行注册");
    LoginHandler loginHandler = new LoginHandler(this);

    public LoginView() {
        super("chatDistance Client");
        Container contentPane = getContentPane();

        nameLabel.setFont(new Font("华文行楷", Font.PLAIN, 40));
        nameLabel.setPreferredSize(new Dimension(0, 40));
        Font centerFont = new Font("楷体", Font.PLAIN, 20);
        userNameLabel.setFont(centerFont);
        userNameField.setPreferredSize(new Dimension(200, 30));
        passwordLabel.setFont(centerFont);
        passwordField.setPreferredSize(new Dimension(200, 30));
        loginButton.setFont(centerFont);
        registerButton.setFont(centerFont);

        centerPanel.add(userNameLabel);
        centerPanel.add(userNameField);
        centerPanel.add(passwordLabel);
        centerPanel.add(passwordField);
        centerPanel.add(loginButton);
        loginButton.addActionListener(loginHandler);
        centerPanel.add(registerButton);
        registerButton.addActionListener(loginHandler);

        layoutConstruct();

        contentPane.add(nameLabel, BorderLayout.NORTH);
        contentPane.add(centerPanel, BorderLayout.CENTER);

        //设置默认按钮
        getRootPane().setDefaultButton(loginButton);
        setSize(400, 300);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);
        setVisible(true);
    }

    public static void main(String[] args) {
        new LoginView();
    }

    private void layoutConstruct() {
        Spring childWidth = Spring.sum(Spring.sum(Spring.width(userNameLabel), Spring.width(userNameField)), Spring.constant(20));
        int offsetX = childWidth.getValue() / 2;

        springLayout.putConstraint(SpringLayout.WEST, userNameLabel, -offsetX
                , SpringLayout.HORIZONTAL_CENTER, centerPanel);
        springLayout.putConstraint(SpringLayout.NORTH, userNameLabel, 20, SpringLayout.NORTH, centerPanel);
        springLayout.putConstraint(SpringLayout.WEST, userNameField, 20, SpringLayout.EAST, userNameLabel);
        springLayout.putConstraint(SpringLayout.NORTH, userNameField, 0, SpringLayout.NORTH, userNameLabel);
        springLayout.putConstraint(SpringLayout.EAST, passwordLabel, 0, SpringLayout.EAST, userNameLabel);
        springLayout.putConstraint(SpringLayout.NORTH, passwordLabel, 20, SpringLayout.SOUTH, userNameLabel);
        springLayout.putConstraint(SpringLayout.WEST, passwordField, 20, SpringLayout.EAST, passwordLabel);
        springLayout.putConstraint(SpringLayout.NORTH, passwordField, 0, SpringLayout.NORTH, passwordLabel);

        springLayout.putConstraint(SpringLayout.WEST, loginButton, 50, SpringLayout.WEST, userNameLabel);
        springLayout.putConstraint(SpringLayout.NORTH, loginButton, 40, SpringLayout.SOUTH, passwordLabel);
        springLayout.putConstraint(SpringLayout.NORTH, registerButton, 0, SpringLayout.NORTH, loginButton);
        springLayout.putConstraint(SpringLayout.WEST, registerButton, 20, SpringLayout.EAST, loginButton);
    }

    public JTextField getUserNameField() {
        return userNameField;
    }

    public JPasswordField getPasswordField() {
        return passwordField;
    }
}